package com.mdd.maduoduo.utils;

import io.reactivex.Observable;
import io.reactivex.subjects.PublishSubject;
import io.reactivex.subjects.Subject;

public class RxBus {
    private static RxBus instance;
    private final Subject<Object> subject = PublishSubject.create().toSerialized();

    private RxBus() {}

    public static synchronized RxBus getInstance() {
        if (instance == null) {
            instance = new RxBus();
        }
        return instance;
    }

    /**
     * 发送事件
     */
    public void post(Object event) {
        subject.onNext(event);
    }

    /**
     * 根据事件类型获取Observable
     */
    public <T> Observable<T> toObservable(Class<T> eventType) {
        return subject.ofType(eventType);
    }
}
